/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChooseUserDialog.java
 *
 * Created on 24/12/2010, 05:22:00 PM
 */

package com.kap.view.user;

import com.kap.services.user.UserDTO;
import com.kap.services.user.UserManager;
import com.kap.services.util.Validator;
import java.awt.BorderLayout;
import javax.swing.JComponent;
import javax.swing.JOptionPane;


/**
 *
 * @author Julian Caicedo
 */
public class ChooseUserDialog extends javax.swing.JDialog {

    private ChooseUserMainPanel main;
    private ChooseUserNewPanel newC;
    private UserDTO user;

    /** Creates new form ChooseUserDialog */
    public ChooseUserDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        endInitialization(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelMain = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Karte Auswendiglernen Programm (KAP)");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jPanelMain.setPreferredSize(new java.awt.Dimension(288, 78));

        javax.swing.GroupLayout jPanelMainLayout = new javax.swing.GroupLayout(jPanelMain);
        jPanelMain.setLayout(jPanelMainLayout);
        jPanelMainLayout.setHorizontalGroup(
            jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 288, Short.MAX_VALUE)
        );
        jPanelMainLayout.setVerticalGroup(
            jPanelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 78, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing


    private void endInitialization(java.awt.Frame parent) {
        main = new ChooseUserMainPanel();
        main.getjButtonAccept().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAcceptActionPerformed(evt);
            }
        });
        main.getjButtonNewUser().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNewUserActionPerformed(evt);
            }
        });
        newC = new ChooseUserNewPanel();
        newC.getjButtonAccept().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAcceptNewActionPerformed(evt);
            }
        });
        newC.getjButtonCancel().addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        changeView(main);
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    private void jButtonAcceptActionPerformed(java.awt.event.ActionEvent evt) {
        user = new UserDTO();
        user.setName((String)main.getjComboBoxUser().getSelectedItem());
        user = UserManager.getUserManager().getUserByName(user);
        dispose();
    }

    private void jButtonNewUserActionPerformed(java.awt.event.ActionEvent evt) {
        newC.clearPanel();
        changeView(newC);
    }

    private void jButtonAcceptNewActionPerformed(java.awt.event.ActionEvent evt) {
        user = new UserDTO();
        String name = (String)newC.getjTextFieldUser().getText().trim();
        if(Validator.isAlphanumeric(name, false, Validator.STANDARD)){
            user.setName(name);
        }
        else{
            JOptionPane.showMessageDialog(this, "Insert a valid alphanumeric name.", "Attention", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(UserManager.getUserManager().insertUser(user)){
            user = UserManager.getUserManager().getUserByName(user);
            dispose();
        }
        else{
            JOptionPane.showMessageDialog(this, "There was an error trying to insert the user.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
    }

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {
        main.clearPanel();
        changeView(main);
    }

    private void changeView(JComponent panel){
        jPanelMain.removeAll();
        jPanelMain.setLayout(new BorderLayout());
        jPanelMain.add(panel);
        jPanelMain.updateUI();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanelMain;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the user
     */
    public UserDTO getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(UserDTO user) {
        this.user = user;
    }



}
